import { StorageManager } from '@vben-core/shared/cache';

import { uploadRequestClient } from '#/api/request';

const storageManager = new StorageManager({
  prefix: '',
  storageType: 'sessionStorage',
});

/**
 * 根据文件的md5获取未上传完的任务
 * @param identifier 文件md5
 * @returns {Promise<AxiosResponse<any>>}
 */
const taskInfo = (identifier: string) => {
  return uploadRequestClient.get(
    `/dsc-systemMS/dsc-file-uploader/${storageManager.getItem('userId')}/${identifier}`,
  );
};

/**
 * 初始化一个分片上传任务
 * @param identifier 文件md5
 * @param fileName 文件名称
 * @param totalSize 文件大小
 * @param chunkSize 分块大小
 * @returns {Promise<AxiosResponse<any>>}
 */
const initTask = ({ identifier, fileName, totalSize, chunkSize }: any) => {
  // console.log(accessToken)
  return uploadRequestClient.post('/dsc-systemMS/dsc-file-uploader', {
    identifier,
    fileName,
    totalSize,
    chunkSize,
    userId: storageManager.getItem('userId'),
  });
};

/**
 * 获取预签名分片上传地址
 * @param identifier 文件md5
 * @param partNumber 分片编号
 * @returns {Promise<AxiosResponse<any>>}
 */
const preSignUrl = ({ identifier, partNumber }: any) => {
  return uploadRequestClient.get(
    `/dsc-systemMS/dsc-file-uploader/${storageManager.getItem('userId')}/${identifier}/${partNumber}`,
  );
};

/**
 * 合并分片
 * @param identifier
 * @returns {Promise<AxiosResponse<any>>}
 */
const merge = (identifier: string) => {
  return uploadRequestClient.post(
    `/dsc-systemMS/dsc-file-uploader/merge/${storageManager.getItem('userId')}/${identifier}`,
  );
};

export default {
  taskInfo,
  initTask,
  preSignUrl,
  merge,
};
